package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eliota.common.R;
import com.eliota.entity.Role;
import com.eliota.entity.User;
import com.eliota.service.RoleService;
import com.eliota.utils.TokenUtil;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 新增
     * @param role
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Role role){
        return R.success(roleService.saveOrUpdate(role));
    }

    @GetMapping("/list")
    public R findAll(){
        return R.success(roleService.list());
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        boolean isDel =  roleService.deleteById(id);
        if(isDel){
            System.out.println(isDel + ">>>>>>>>>>>>>.删除成功");
            return R.success();
        }else {
            System.out.println("》》》》》》》》》》》》删除失败");
            return R.error("删除失败");
        }
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        return R.success(roleService.deleteBathByIds(ids));
    }

    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String roleName){
        Page<Role> rolePage = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.like("role_name",roleName);

        return R.success(roleService.page(rolePage,roleQueryWrapper));
    }


    /**
     * 绑定角色id和菜单ids的关系
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public R saveRoleMenu(@PathVariable Integer roleId,@RequestBody List<Integer> menuIds){
        roleService.setRoleMenu(roleId,menuIds);
        return R.success();
    }

    @GetMapping("/roleMenu/{roleId}")
    public R getRoleMenu(@PathVariable Integer roleId){
        return R.success(roleService.getRoleMenu(roleId));
    }
}
